package studio.raptor.ddal.core.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.core.log.bo.SqlProcessLogData;
import studio.raptor.ddal.core.log.service.impl.LogProcessImpl;

/**
 * DDAL 监控日志处理工具类，并且不直接包含日志处理的实现细节。
 * 是否开启监控日志处理功能依赖于当前classpath中是否引用"ddal-logclient"实现。
 * Created by dell on 2018/1/15.
 */
public class LogClientUtil {

    private static Logger logger = LoggerFactory.getLogger(LogClientUtil.class);
    private Object LogProcessor=null;
    private boolean isNeedProcess=false;
    private AsyncMonitorProcessor monitorProcessor=null;

    private static final LogClientUtil logClientUtil=new LogClientUtil();

    private LogClientUtil(){
        try {
            LogProcessor= Class.forName("studio.raptor.ddal.core.log.service.impl.LogProcessImpl").newInstance();
            isNeedProcess=true;
            monitorProcessor=new AsyncMonitorProcessor();
        } catch (Exception e) {
            e.printStackTrace();
            LogProcessor=null;
            isNeedProcess=false;
        }
    }

    public static void process(Object logObj){
        if(logClientUtil.LogProcessor!=null){
            LogProcessImpl logProcess=(LogProcessImpl)logClientUtil.LogProcessor;
            logProcess.process( logObj );
        }
        else{
            logger.info( "\n logClientUtil.LogProcessor=null" );
        }
    }

    public static SqlProcessLogData getSqlProcessLogData() {
        if(logClientUtil.isNeedProcess) return new SqlProcessLogData();
        return null;
    }
}
